<?php

namespace App\Http\Controllers;

use App\Helpers\TLSSigAPIv2;
use App\Models\Customer;
use App\User;
use Illuminate\Http\Request;

class TimController extends Controller
{
    public function init(TLSSigAPIv2 $sig)
    {
        return [
            'me' => [
                'id' => (string) authUserId(),
                'username' => authUsername(),
                'status' => 'online',
                'sign' => '在深邃的编码世界，做一枚轻盈的纸飞机',
                'avatar' => 'https://ali-static.oss-cn-beijing.aliyuncs.com/default.png',
            ],
            'users' => $this->getUsers(),
            // 腾讯云通讯参数, 固定, 不可更改
            'tim' => [
                'SDKAppID' => config('tim.appid'),
                'userID' => (string) authUserId(),
                '_token' => csrf_token(),
            ],
        ];
    }

    public function sign(TLSSigAPIv2 $APIv2)
    {
        return $APIv2->genSig(authUserId());
    }

    protected function getUsers()
    {
        $users = [];

        foreach (User::where('id', '<>', auth()->id())->get() as $user) {
            array_push($users, [
                'id' => (string) $user->id,
                'name' => $user->name,
                'avatar' => 'https://ali-static.oss-cn-beijing.aliyuncs.com/default.png'
            ]);
        }

        return collect($users)->pluck('id');
    }

    protected function getCustomers()
    {
        $customers = [];

        foreach (Customer::all() as $customer) {
            array_push($customers, [
                'id' => 'customer-' . $customer->id,
                'name' => $customer->username,
                'avatar' => 'https://ali-static.oss-cn-beijing.aliyuncs.com/default.png'
            ]);
        }

        return collect($users)->pluck('id');
    }
}
